fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. // 3 sum problems
  5. class Solution {
  6. public:
  7. vector<vector<int>> threeSum(vector<int>& nums) {
  8. // create a vector which stores the index of the elements which forms the sum equal to zero
  9. vector<vector<int>> res;
  10.  
  11. // Run the loop for the given equation
  12. // create a function which stores the
  13. for(int l=0;l<nums.size()-2;l++){
  14. // Run the another loop for the given problems
  15. for(int m=l+1;m<nums.size();m++){
  16. // Run another loop for the given problems
  17. for(int n=m+1;n<nums.size();n++){
  18. // cout<<"l: "<< l <<" m: "<< m <<" n: "<< n<<endl;
  19. if(l!=m && l!=n && m!=n){
  20. int sum=nums[l]+nums[m]+nums[n];
  21. if(sum==0){
  22. vector<int> v;
  23. v.push_back(nums[l]);
  24. v.push_back(nums[m]);
  25. v.push_back(nums[n]);
  26. res.push_back(v);
  27.  
  28. }
  29.  
  30.  
  31. }
  32. }
  33. }
  34. }
  35. return res;
  36. }
  37. };
  38. int main() {
  39. // your code goes here
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Standard output is empty